1796C - Maximum Set - CodeForces Solution


binary search dp math

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double lld;
#define pll pair<ll,ll>
#define vll vector<ll>
#define  ln "\n"
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define be begin
#define ub upper_bound
#define lb lower_bound
#define bi binary_search
#define sll set <ll>
#define msll multiset <ll>
#define vpll vector <pair<ll,ll>>
#define mll  map <ll,ll>
#define all(v) v.begin(),v.end()
#define mem1(a)  memset(a,-1,sizeof(a))
#define mem0(a)  memset(a,0,sizeof(a))
#define i1(x) cin>>x
#define i2(x1,x2) cin>>x1>>x2
#define i3(x1,x2,x3) cin>>x1>>x2>>x3
#define i4(x1,x2,x3,x4) cin>>x1>>x2>>x3>>x4
#define o1(x) cout<<x<<ln
#define o2(x1,x2) cout<<x1<<" "<<x2<<ln
#define o3(x1,x2,x3) cout<<x1<<" "<<x2<<" "<<x3<<ln
#define o4(x1,x2,x3,x4) cout<<x1<<" "<<x2<<" "<<x3<<" "<<x4<<ln
#define rep(i,s,e) for(ll i=s;i<e;i++)
#define rrep(i,s,e) for(ll i=s-1;i>=e;i--)
#define geta(a,n) for(ll i=0;i<n;i++)cin>>a[i];
const ll mod = 998244353;
const ll infinity = 1e18;

void solve()
{
	ll l,r;
	i2(l,r);
	ll mx=1;
	ll sz=1;
	while(1)
	{
		ll val=l*pow(2,mx);
		if(val>r)
			break;
		sz++;
		mx++;
	}
	if(sz==1)
	{
		o2(1,r-l+1);
		return;
	}
	ll size=sz;
	mx--;
	ll ratio=pow(2,mx);
	ll left=l,right=r;
	ll we=-1;
	ll ct=0;
	//o1(ratio);
	while(left<=right)
	{
		// ct++;
		// if(ct>10)
		// 	break;
		
		ll mid=(left+right)/2;
		
		ll vv=r/mid;
		//o4(left,right,mid,vv);
		if(vv<ratio)
		{
			right=mid-1;
		}
		else
		{
			we=mid;
			left=mid+1;
		}
	}
	ratio=(ratio*3)/2;
	ll wee=-1;
	left=l,right=r;
	while(left<=right)
	{
		// ct++;
		// if(ct>20)
		// 	break;
		
		ll mid=(left+right)/2;
		//o3(left,right,mid);
		ll vv=r/mid;
		if(vv<ratio)
		{
			right=mid-1;
		}
		else
		{
			wee=mid;
			left=mid+1;
		}
	}
	//o2(we,wee);
	ll ans;
	if(wee==-1)
	{
		ans=(we-l+1)%mod;
		o2(size,ans);
		return;
	}
	ll ft=wee-l+1;
	ll st=we-wee;

	ans=(ft*(size))%mod;
	ans=(ans+st)%mod;
	o2(size,ans);

}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	ll t = 1;
	cin >> t;

	srand(time(0));
	for (ll i = 1; i <= t; i++)
	{

		//cout << "Case #" << i << ": ";

		solve();

	}
	return 0;
}


Comments

Submit
0 Comments
More Questions

1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number
228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality
1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game
248A - Cupboards
1641A - Great Sequence
1537A - Arithmetic Array
1370A - Maximum GCD
149A - Business trip
34A - Reconnaissance 2
59A - Word
462B - Appleman and Card Game
1560C - Infinity Table
1605C - Dominant Character
1399A - Remove Smallest
208A - Dubstep
1581A - CQXYM Count Permutations
337A - Puzzles